package com.example.weatherdemotest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weatherdemotest.R;
import com.example.weatherdemotest.bean.DayWeatherBean;

import java.util.List;

/**
 * Adapter<>里面包含的是ViewHolder
 */
public class FutureWeatherAdapter extends RecyclerView.Adapter<FutureWeatherAdapter.WeatherViewHolder> {

    private Context mContext; //上下文
    private List<DayWeatherBean> mWeatherBeans; //每一天的数据

    //添加构造方法
    public FutureWeatherAdapter(Context context, List<DayWeatherBean> weatherBeans) {
        this.mContext = context;
        this.mWeatherBeans = weatherBeans;
    }

    /**
     * 创建viewHolder
     *
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param viewType The view type of the new View.
     * @return
     */
    @NonNull
    @Override
    public WeatherViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.weather_item_layout, parent, false);
        WeatherViewHolder weatherViewHolder = new WeatherViewHolder(view);
        return weatherViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull WeatherViewHolder holder, int position) {
        //1、拿到当前位置的Bean对象
        DayWeatherBean weatherBean = mWeatherBeans.get(position);
        //2、设置控件中的文本信息
        holder.ivWeatherIcon.setImageResource(getWeatherIcon(weatherBean.getWeatherIcon()));
//        holder.tvTemperature.setText(weatherBean.getWeatherTemDay() + "°C");
        holder.tvWeatherContent.setText(weatherBean.getWeather());
        holder.tvDate.setText(weatherBean.getDate());
        holder.tvLowHigh.setText(weatherBean.getWeatherTemNight() + "°C ~" + weatherBean.getWeatherTemDay() + "°C");
//        holder.tvWind.setText(weatherBean.getWind() + " " + weatherBean.getWindspeed());
    }

    /**
     * 如果mWeatherBeans为空，返回0，否则，返回mWeatherBeans.size()
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return (mWeatherBeans == null) ? 0 : mWeatherBeans.size();
    }

    /**
     * 自定义ViewHolder 示例化每个控件
     */
    class WeatherViewHolder extends RecyclerView.ViewHolder {
        ImageView ivWeatherIcon;
        TextView tvTemperature, tvWeatherContent, tvDate, tvLowHigh, tvWind;

        public WeatherViewHolder(@NonNull View itemView) {
            super(itemView); //该view为WeatherViewHolder中绑定的R.layout.weather_item_layout的view

            ivWeatherIcon = (ImageView) itemView.findViewById(R.id.iv_weather_icon);
//            tvTemperature = (TextView) itemView.findViewById(R.id.tv_weather_temperature);
            tvWeatherContent = (TextView) itemView.findViewById(R.id.tv_weather_content);
            tvDate = (TextView) itemView.findViewById(R.id.tv_date);
            tvLowHigh = (TextView) itemView.findViewById(R.id.tv_weather_low_high);
//            tvWind = (TextView) itemView.findViewById(R.id.tv_weather_wind);
        }
    }

    /**
     * 根据返回的天气情况设置不同的天气图标
     * 固定9种类型(您也可以根据wea字段自己处理):
     * *      xue(icon_weather_daxue)、
     * *      lei(icon_weather_leiyu)、
     * *      shachen(icon_weather_shachen)、
     * *      wu(icon_weather_wumai)、
     * *      bingbao(icon_weather_bingbao)、
     * *      yun(icon_weather_duoyun)、
     * *      yu(icon_weather_zhongyu)、
     * *      yin(icon_weather_yin)、
     * *      qing(icon_weather_qing)
     *
     * @param weaStr
     * @return
     */
    private int getWeatherIcon(String weaStr) {
        int result = 0;
        switch (weaStr) {
            case "xue":
                result = R.drawable.icon_weather_daxue;
                break;
            case "lei":
                result = R.drawable.icon_weather_leiyu;
                break;
            case "shachen":
                result = R.drawable.icon_weather_shachen;
                break;
            case "wu":
                result = R.drawable.icon_weather_wumai;
                break;
            case "bingbao":
                result = R.drawable.icon_weather_bingbao;
                break;
            case "yun":
                result = R.drawable.icon_weather_duoyun;
                break;
            case "yu":
                result = R.drawable.icon_weather_zhongyu;
                break;
            case "yin":
                result = R.drawable.icon_weather_yin;
                break;
            case "qing":
                result = R.drawable.icon_weather_qing;
                break;
            default:
                result = R.drawable.icon_weather_loading;
                break;
        }
        return result;
    }
}
